﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.ManagerPage
{
    public partial class ManageEmployees : System.Web.UI.Page
    {
        LoginDetails details;
        List<Employee> list;
        protected void Page_Load(object sender, EventArgs e)
        {
            details = (LoginDetails)Session["managerDetails"];
            try
            {
                if (details == null || !details.isManager)
                {
                    //hack attempt
                    Response.Redirect("~/General/unAuthorized.aspx");
                }
            }
            catch
            {
                //hack attempt
                Response.Redirect("~/General/unAuthorized.aspx");
            }

            if (Session["errMsg"] != null)
            {
                this.eMsgText.Text = (string)Session["errMsg"];
                this.eMsgText.Visible = true;
                Session["errMsg"] = null;
            }
            else
            {

                //create a button for each employee
                list=null;
                try
                {
                    list = BL.getAllEmployeesByTeam(BL.getEmployeeById(details.ID).managerOf,false);
                }
                catch (NotImplementedException)
                {
                    //there are no employees to manage...
                    Session["errMsg"] = "You have no Employees to manage!";
                    Response.Redirect("~/ManagerPage/ManageEmployees.aspx");
                }

                if (list == null)
                {
                    Session["errMsg"] = "Unexpected Error occured...";
                    Response.Redirect("~/ManagerPage/ManageEmployees.aspx");
                }
                int counter = 0;
                Control mainDivider = this.mainDiv;
                foreach (Employee emp in list)
                {
                    counter++;
                    Control c1 = mainDivider.FindControl("emp" + counter.ToString());
                    if (c1 != null)
                    {
                        if ( !emp.isAdmin.Equals("True") && !emp.isManager.Equals("True"))
                        {
                            ((Button)c1).Visible = true;
                            ((Button)c1).Text = emp.firstName + " " + emp.lastName;
                            ((Button)c1).CommandName = emp.id.ToString();
                        }
                    }
                }

            }

            initLogoutButton();
        }

        private void initLogoutButton()
        {
            //experimental logout feature
            Control h = this.Header.FindControl("LoginView1");
            Control h2 = h.FindControl("loginLink");
            ((System.Web.UI.HtmlControls.HtmlAnchor)h2).HRef = "./Account/Login.aspx";
            ((System.Web.UI.HtmlControls.HtmlAnchor)h2).InnerText = "Log Out";
        }

        protected void ManageEmp(object sender, EventArgs e)
        {
            LoginDetails empToManage = new LoginDetails(((Button)sender).CommandName,false,false);
            Session["myobject"] = empToManage;
            Response.Redirect("~/EmployeePage/EmployeePage.aspx");
        }


    }
    
}